package com.atom.module.coroutine.test

import com.atom.module.coroutine.core.RecursiveList
import com.atom.module.coroutine.core.forEach
import com.atom.module.coroutine.core.loopOn
import com.atom.module.coroutine.core.remove

fun main() {

    test3()
}

fun test1() {
    var head: RecursiveList.Cons<String>? = null
    var end: RecursiveList.Cons<String>? = null

    repeat(10) { index ->

        if (head == null) {
            println("head == null $index")
            head = RecursiveList.Cons<String>(index.toString(), RecursiveList.Nil)
            end = head
        } else {
            println("head != null $index")
            end = RecursiveList.Cons<String>(index.toString(), end!!)
        }
    }

    end?.forEach {
        print(it)
    }
}

fun test2() {
    var head: RecursiveList.Cons<Any>? = null
    var end: RecursiveList.Cons<Any>? = null

    repeat(10) { index ->

        if (head == null) {
            println("head == null $index")
            head = RecursiveList.Cons<Any>(index.toString(), RecursiveList.Nil)
            end = head
        } else {
            println("head != null $index")
            end = if (index % 2 == 0) {
                RecursiveList.Cons(index, end!!)
            } else {
                RecursiveList.Cons(index.toString(), end!!)
            }
        }
    }

    end?.loopOn<Int> {
        print(it)
    }
}

fun test3() {
    var head: RecursiveList.Cons<String>? = null
    var end: RecursiveList.Cons<String>? = null

    repeat(10) { index ->

        if (head == null) {
            println("head == null $index")
            head = RecursiveList.Cons<String>(index.toString(), RecursiveList.Nil)
            end = head
        } else {
            println("head != null $index")
            end = RecursiveList.Cons<String>(index.toString(), end!!)
        }
    }

    val remove = end?.remove("6")
    println(remove)
}